package com.zygk.xmpp.xmpp;

import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.zygk.library.util.ImageUtil;
import com.zygk.xmpp.util.Constants;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smackx.muc.MultiUserChat;
import org.jivesoftware.smackx.packet.VCard;
import org.json.JSONObject;

import java.util.List;

import com.zygk.xmpp.model.M_Room;

/**
 * XMPP工具类
 * 2018/7/25 0025 下午 4:24
 */
public class XMPP {
  public static final String TAG = XMPP.class.getSimpleName();
  public static Context context;

  public enum BODY_TYPE {
    TEXT("0"), IMG("2"), RECORD("3"), VIDEO("5"), POSITION("6");
    private String decription;

    /*私有的构造函数，给每个枚举值设置一个描述*/
    private BODY_TYPE(String str) {
      this.decription = str;
    }
  }

  /**
   * 登陆XMPP
   */
  public static void loginXmpp(final Context mContext,Context ApplicationContext, final String userName,
      final String password,final Intent intent) {
    context = ApplicationContext;
    new XmppLoadThread(mContext) {

      @Override protected Object load() {
        boolean isSuccess = XmppConnection.getInstance().login(userName, password);
        return isSuccess;
      }

      @Override protected void result(Object o) {
        boolean isSuccess = (Boolean) o;
        if (isSuccess) {
          Log.i(TAG, "登录xmpp--->成功");
          Constants.XMPP_USERNAME = userName;
          Constants.XMPP_PASSWORD = password;
          Constants.XMPP_NICKNAME = MessageConstants.getNameById(userName);
          Presence presence = new Presence(Presence.Type.available);
          presence.setMode(Presence.Mode.available);
          XmppConnection.getInstance().sendPacket(presence);
          mContext.startActivity(intent);
        } else {
          Log.i(TAG, "登录xmpp--->失败");
        }
      }
    };
  }

  /**
   * 登出xmpp
   */
  public void logoutXmpp(Context mContext) {
    XmppConnection.getInstance().closeConn();
    closeConnection();

    NotificationManager nm =
        (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    nm.cancelAll();

    Log.i(TAG, "logout====success");
  }

  /**
   * 获取用户信息
   */
  public static void getUserInfo(String user) {
    XmppConnection.getInstance().getUserInfo(user);
  }

  public static void deleteOfflineMsg() {
    XmppConnection.getInstance().deleteOfflineMsg();
  }

  public static void closeConnection() {
    XmppConnection.getInstance().closeConnection();
  }

  public static void reconnect() {
    XmppConnection.getInstance().reconnect();
  }

  public static void clearData() {
    XmppConnection.getInstance().clearData();
  }

  public static void closeXmppService() {
    XmppConnection.getInstance().closeXmppService();
  }

  public static void setNotifyClass(Class notifyClass) {
    XmppConnection.getInstance().setNotifyClass(notifyClass);
  }

  public static void setReceiver(String chatName, int chatType) {
    XmppConnection.getInstance().setRecevier(chatName, chatType);
  }

  /**
   * 发送文字，表情
   */
  public static void sendTextMsg(String msg, int chatType) {
    try {
      JSONObject obj = new JSONObject();
      obj.put("msg", msg);
      obj.put("bodyType", BODY_TYPE.TEXT.decription);
      XmppConnection.getInstance().sendMsg(obj.toString(), chatType);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * 发送图片
   */
  public static void sendImgMsg(String imgPath, int chatType) {
    try {
      JSONObject obj = new JSONObject();
      obj.put("msg", imgPath);
      obj.put("bodyType", BODY_TYPE.IMG.decription);
      XmppConnection.getInstance().sendMsg(obj.toString(), chatType);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * 发送录音
   */
  public static void sendRecordMsg(String audioPath, int chatType) {
    try {
      JSONObject obj = new JSONObject();
      obj.put("msg", ImageUtil.getBase64StringFromFile(audioPath));
      obj.put("bodyType", BODY_TYPE.RECORD.decription);
      XmppConnection.getInstance().sendMsg(obj.toString(), chatType);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * 发送小视频
   */
  public static void sendVideoMsg(String msg, int chatType) {
    try {
      JSONObject obj = new JSONObject();
      obj.put("msg", msg);
      obj.put("bodyType", BODY_TYPE.VIDEO.decription);
      XmppConnection.getInstance().sendMsg(obj.toString(), chatType);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * 发送位置信息
   */
  public static void sendPositionMsg(String lat, String lon, String name, String address,
      String imageUrl, int chatType) {
    try {
      JSONObject obj = new JSONObject();
      obj.put("lat", lat);
      obj.put("lon", lon);
      obj.put("name", name);
      obj.put("address", address);
      obj.put("imageUrl", imageUrl);
      obj.put("bodyType", BODY_TYPE.POSITION.decription);
      XmppConnection.getInstance().sendMsg(obj.toString(), chatType);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static void searchMember(String key) {
    XmppConnection.getInstance().searchMember(key);
  }

  public static boolean addUser(String id, String userName) {
    return XmppConnection.getInstance().addUser(id, userName);
  }

  public static void removeUser(String userName) {
    XmppConnection.getInstance().removeUser(userName);
  }

  public static boolean changeVcard(VCard vcard) {
    return XmppConnection.getInstance().changeVcard(vcard);
  }

  public static void getUservcard(String user) {
    XmppConnection.getInstance().getUservcard(user);
  }

  public static MultiUserChat createRoom(String roomName, String description) {
    return XmppConnection.getInstance().createRoom(roomName, description);
  }

  public static List<M_Room> getMyRoom() {
    return XmppConnection.getInstance().getMyRoom();
  }

  public static void getUserinfo() {
    XmppConnection.getInstance().getUserinfo();
  }
}
